package com.xs.smc.controller;

import com.xs.smc.entity.Order;
import com.xs.smc.enums.OrderEvent;
import com.xs.smc.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 接单操作（CONFIRM）
     */
    @PostMapping("/{id}/confirm")
    public ResponseEntity<?> confirmOrder(@PathVariable Long id) {
        Order order = orderService.getOrderById(id);
        if (order == null) {
            return ResponseEntity.badRequest().body("订单不存在");
        }
        boolean result = orderService.processOrder(order, OrderEvent.CONFIRM);
        if (result) {
            // 建议在action里持久化订单状态和相关业务
            return ResponseEntity.ok("接单成功");
        } else {
            return ResponseEntity.badRequest().body("订单当前状态无法接单");
        }
    }

    /**
     * 制作操作（PREPARE）
     */
    @PostMapping("/{id}/prepare")
    public ResponseEntity<?> prepareOrder(@PathVariable Long id) {
        Order order = orderService.getOrderById(id);
        if (order == null) {
            return ResponseEntity.badRequest().body("订单不存在");
        }
        boolean result = orderService.processOrder(order, OrderEvent.PREPARE);
        if (result) {
            return ResponseEntity.ok("开始制作！");
        } else {
            return ResponseEntity.badRequest().body("当前状态无法开始制作");
        }
    }

    // 其它操作类似
}